package com.itbaizhan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.domain.AccountInfo;
import com.itbaizhan.domain.PayInfo;
import com.itbaizhan.mapper.PayInfoMapper;
import com.itbaizhan.openfeign.PaymentService;
import com.itbaizhan.service.AccountInfoService;
import com.itbaizhan.mapper.AccountInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 13487
* @description 针对表【account_info】的数据库操作Service实现
* @createDate 2024-04-23 16:26:48
*/
@Service
public class AccountInfoServiceImpl implements AccountInfoService{

    @Autowired
    private AccountInfoMapper accountInfoMapper;
    @Autowired
    private PayInfoMapper payInfoMapper;
    @Autowired
    private PaymentService paymentService;

    @Override
    public void updataAccount(PayInfo payInfo) {
        // 接口幂等性
        PayInfo payInfo1 = payInfoMapper.selectById(payInfo.getTxNo());
        if (payInfo1 != null){
            return;
        }

        LambdaQueryWrapper<AccountInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AccountInfo::getAccountNo, payInfo.getAccountNo());

        AccountInfo accountInfo = accountInfoMapper.selectOne(lambdaQueryWrapper);
        if (accountInfo != null){
            accountInfo.setAccountBalance(accountInfo.getAccountBalance().add(payInfo.getPayAmount()));
            accountInfoMapper.updateById(accountInfo);
        }

        payInfoMapper.insert(payInfo);
    }

    @Override
    public String findPayInfo(String txNo) {
        return paymentService.query(txNo);
    }
}




